package model;

import java.io.File;
import java.util.HashSet;
import java.util.Set;

import org.tartarus.snowball.SnowballStemmer;

import controller.Utils;

public class PreProcessador {
	
	private static PreProcessador instance;
	private SnowballStemmer stemmer;
	private Set<String> stopWords;
	
	public static synchronized PreProcessador getInstance(){
		if (instance == null){
			instance = new PreProcessador();
		}
		return instance;
	}
	
	@SuppressWarnings("unchecked")
	private PreProcessador() {
		
		// iniciando stemmer
		String idioma = "english";
		SnowballStemmer stemmer;
		try {
			Class stemClass = Class.forName("org.tartarus.snowball.ext." + idioma + "Stemmer");
			stemmer = (SnowballStemmer) stemClass.newInstance();
		} catch (ClassNotFoundException e) {
			throw new RuntimeException(e);
		} catch (InstantiationException e) {
			throw new RuntimeException(e);
		} catch (IllegalAccessException e) {
			throw new RuntimeException(e);
		}
		this.stemmer = stemmer;
		
		//carregando stopwords
		stopWords = new HashSet<String>();
		String estringao = Utils.getFileAsString(new File("se/stopwords_en.txt"));
		String[] split = estringao.split("[\\s]+");
		for (int i = 0; i < split.length; i++) {
			stopWords.add(split[i]);
		}
		
	}
	
	public String lowerCase(String word){
		return word.toLowerCase();
	}

	public String stem(String word) {
		stemmer.setCurrent(word);
		stemmer.stem();
		return stemmer.getCurrent();
	}
	
	public boolean isStopWord(String word){
		return stopWords.contains(word);
	}

}
